;$(function () {
    getList();
})


function getList(pageNo, pageSize,startTimeStr,keyWord) {
    if (typeof(pageNo) == 'undefined') {
        pageNo = 1;
    }

    if (typeof(pageSize) == 'undefined') {
        pageSize = 15;
    }
    $.ajax({
        url: ctxPath+"/party/meeting/getPage",
        type: 'POST',
        data: {pageNo: pageNo, pageSize: pageSize,startTimeStr:startTimeStr,title:keyWord},
        success: function (d) {
            if (d.data != null) {
                var html = "";
                $.each(d.data.dataList, function (i, item) {
                    html += toHtml(item);
                })
                $("#list_result").html(html);
            }
            getPage(d.data.pages, d.data.total,startTimeStr,keyWord);
        }
    });
}

//分页
function getPage(pageCount, totalCount,startTimeStr,keyWord) {
    kkpager.total = pageCount;//总页码
    kkpager.totalRecords = totalCount;//总条数
    kkpager.generPageHtml({
        pno: 0,
        mode: 'click',//默认值是link，可选link或者click
        click: function (n) {
            getList(n, 15,startTimeStr,keyWord);
            this.selectPage(n);
            return false;
        }
    });
}

function toHtml(data) {

    var edit = "<button type='button' class='btn btn-primary btn-sm' onclick='edit(" + data.id + ")'><i class=\"fa fa-edit\"></i>编辑</button>";
    var del = "<button type='button' class='btn btn-danger btn-sm' onclick='del(" + data.id + ")'><i class=\"fa fa-trash-o\"></i>删除</button>";
    var detail = "<button type='button' class='btn btn-info btn-sm' onclick='detail(" + data.id + ")'><i class=\"fa fa-rocket\"></i>详情</button>";
    var sign = "<button type='button' class='btn btn-success btn-sm' onclick='sign(" + data.id + ")'><i class=\"fa fa-rocket\"></i>签到码</button>";
    var startTime = new Date(data.startTime).Format("yyyy-MM-dd hh:mm");
    var endTime = new Date(data.endTime).Format("yyyy-MM-dd hh:mm");

    return '<tr>'
        + '<td>'
        + data.title
        + '</td>'
        + '<td class="text-center">'
        + startTime+'-'+endTime
        + '</td>'
        + '<td class="text-center">'
        + data.site
        + '</td>'
        + '<td class="text-center">'
        + edit
        + del
        + detail
        + sign
        + '</td>'
        + '</tr>';
}

function sign(id){
    $("#qrcode").empty();
    //待提供接口后替换
    $("#qrcode").qrcode("http://localhost:8080/"+id);
    $("#myModal").modal('show');
}

function del(id) {
    $.ajax({
        url: ctxPath+"/party/meeting/del",
        type: 'POST',
        data: {id:id},
        success: function (d) {
            if(d.success){
                alert(d.message);
            }

            getList();
        },
        error: function () {
            alert("异常！");
        }
    });
}

function edit(id) {

    window.location.href = ctxPath+"/party/meeting/editNews/"+id;
}

function detail(id){
    window.location.href = ctxPath+"/party/meeting/detail/"+id;
}

function search(){
    var startTimeStr = $("#startTimeStr").val();
    var keyWord = $("#keyWord").val();
    getList(1,15,startTimeStr,keyWord);
}